﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using eCommerce.db.DataRespository;
using eCommerce.db.SQLDataAccess;

namespace eCommerce.db.DALRespository
{
    public class NoteDal
    {
        #region SizeDal private
        private static void SetInfo(IDataRecord reader, ref NoteEntity o)
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                PropertyInfo propertyInfo = DbCommon.GetPropertyInfo(o, reader.GetName(i));
                if (((propertyInfo != null) && (reader[i] != null)) && (reader[i] != DBNull.Value))
                    propertyInfo.SetValue(o, reader[i], null);
                else
                    o[reader.GetName(i)] = reader[i];
            }
        }

        private static void SetInfoList(ref SqlDataReader reader, ref List<NoteEntity> noteList)
        {
            while (reader.Read())
            {
                var o = new NoteEntity();
                SetInfo(reader, ref o);
                noteList.Add(o);
            }
            reader.Dispose();
        }
        #endregion

        public static List<NoteEntity> GetByStatus(int status, string userName)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstNote = new List<NoteEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Note_GetByStatus", status, userName);
                SetInfoList(ref reader, ref lstNote);
                return lstNote;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Note_GetByStatus: {0}", ex.Message));
            }
        }

        public static NoteEntity GetById(int id)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var o = new NoteEntity();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Note_GetById", id);
                if (reader.Read())
                    SetInfo(reader, ref o);
                return o;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Note_GetById: {0}", ex.Message));
            }
        }

        public static void Save(NoteEntity o)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                if (o.NoteId > 0)
                {
                    SqlHelper.ExecuteNonQuery(
                        DbCommon.ConnectionString,
                        DbCommon.DatabaseSchema + "Note_Update", o.NoteId, o.Title, o.Content);
                }
                else
                {
                    SqlHelper.ExecuteNonQuery(
                        DbCommon.ConnectionString,
                        DbCommon.DatabaseSchema + "Note_Create", o.Title, o.Content, o.CreatedBy, o.Status);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "Note Save: {0}", ex.Message));
            }
        }

        public static void Delete(int id)
        {
            SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "Note_Delete", id);
        }
    }
}
